package com.oreilly.ez.account;

public class Quadratic {

    public static void main(String[] args) {

        double a = 1;
        double b = 2;
        double c = -4;
        double[] solutions = getSolutions(a, b, c);
        for (double solution : solutions) {
            System.out.println(solution);
        }

    }

    /**
     *
     * @param a
     * @param b
     * @param c
     * @return  x1, x2 in an array  [-1.2, 3]
     */
    private static double[] getSolutions(double a, double b, double c) {

        double delta = b * b - 4 * a * c;
        if (delta < 0) {
            throw new RuntimeException("no real solutions");
        }

        double sqrtDelta = Math.sqrt(delta);

        double x1 = (-b + sqrtDelta) / 2 * a;
        double x2 = (-b - sqrtDelta) / 2 * a;

        return new double[]{x1, x2};

    }
}
